package com.leapp.sk.viewdelegate.classs

import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import cc.vv.lkbasecomponent.util.LKSPUtil
import cc.vv.lkbasecomponent.util.LKScreenUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.mvp.bean.ClassMemberInfoEntity
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.GlideCircleTransform
import com.example.mvp.view.LKCircleImageView
import com.leapp.sk.R
import com.example.util.finallist.KEY_USERID


/**
 * 班主任详情
 * @author  chaohaipeng  on 2018/8/1.
 */
class HeadmasterDetialDelegate: BaseAppDelegate() {

    /**
     * 老师头像加载操作
     */
    private val mHeadTRequestOptions: RequestOptions = RequestOptions()

    /**
     * 老师背景头像加载操作
     */
    private val mBRequestOptions: RequestOptions = RequestOptions()
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_head_master_detial
    }

    /**
     * 初始化数据
     */
    fun initData(){
        val rl_ahmd_head= getView<RelativeLayout>(R.id.rl_ahmd_head)
        val layoutParams:LinearLayout.LayoutParams = rl_ahmd_head.layoutParams as LinearLayout.LayoutParams
        layoutParams.width=LKScreenUtil.getScreenWidth()
        layoutParams.height=LKScreenUtil.getScreenWidth()*3/5
        rl_ahmd_head.layoutParams=layoutParams
    }
    /**
     * 设置数据
     */
    fun setData(context: Context, data: ClassMemberInfoEntity?){
        val tv_ahmd_send_message= getView<TextView>(R.id.tv_ahmd_send_message)
        val tv_ahmd_play_pohone= getView<TextView>(R.id.tv_ahmd_play_pohone)
        val stringId = LKSPUtil.getInstance().getString(KEY_USERID)
        if (TextUtils.equals(stringId,data?.id)){
            tv_ahmd_send_message.visibility= View.GONE
            tv_ahmd_play_pohone.visibility= View.GONE
        }else{
            tv_ahmd_send_message.visibility= View.VISIBLE
            tv_ahmd_play_pohone.visibility= View.VISIBLE
        }

        val tv_ahmd_personal_intruct= getView<TextView>(R.id.tv_ahmd_personal_intruct)

        val ima_ahmd_teacher_avatars= getView<LKCircleImageView>(R.id.ima_ahmd_teacher_avatars)
        val tv_ahmd_techer_name= getView<TextView>(R.id.tv_ahmd_techer_name)
        if (data!=null){
            setHead(context,data.avatar,ima_ahmd_teacher_avatars)
            tv_ahmd_techer_name.text=data.nickName
            tv_ahmd_personal_intruct.text=data?.introduction
        }
     }
    /**
     * 设置头像和昵称
     */
    private fun setHead(context: Context, headUrl: String?, headImage: LKCircleImageView) {
        setHeadBackground(headUrl)
        val mRequestManager = Glide.with(context)
        mHeadTRequestOptions?.diskCacheStrategy(DiskCacheStrategy.NONE)
        mHeadTRequestOptions?.skipMemoryCache(true)
        mHeadTRequestOptions?.error(R.mipmap.test_class_member)
        mHeadTRequestOptions?.transforms(GlideCircleTransform(context))
        mRequestManager?.load(headUrl)
                ?.thumbnail(0.2f)
                ?.apply(mHeadTRequestOptions)
                ?.into(headImage)

    }
    fun setHeadBackground(headUrl: String?){
        val iv_ahmd_head_background = getView<ImageView>(R.id.iv_ahmd_head_background)
        val tag = iv_ahmd_head_background.getTag(R.id.tag_head)
        if ( tag==null||!tag?.equals(headUrl)) {
            val mRequestManager = Glide.with(getActivity<AppCompatActivity>())
            mBRequestOptions.diskCacheStrategy(DiskCacheStrategy.NONE)
            mBRequestOptions.skipMemoryCache(true)
            mBRequestOptions.error(R.mipmap.icon_techer_detial)
            mRequestManager?.load(headUrl)
                    ?.thumbnail(0.2f)
                    ?.apply(mHeadTRequestOptions)
                    ?.into(iv_ahmd_head_background)
            iv_ahmd_head_background.setTag(R.id.tag_head,headUrl)
        }
    }
}